package demo08;

/**
 * @author: WangPeng
 * @createTime: 2022/11/01 22:01
 * @description:
 * @version: v1.0
 */
public class BankCard {
    private double balance;
    private boolean flag = false;
    //存钱
    public synchronized void cun(double money){
        System.out.println(Thread.currentThread().getName());
        if (flag == true){
            try {
                wait();
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        balance = balance + money;
        System.out.println(Thread.currentThread().getName()+"存入了"+money);
        flag = true;
        notify();
    }
    //取钱
    public synchronized void qu(double money){
        System.out.println(Thread.currentThread().getName());
        if (flag == true){
            try {
                wait();
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        balance = balance - money;
        System.out.println(Thread.currentThread().getName()+"存入了"+money);
        flag = true;
        notify();
    }
}
